import React from "react";

import { ajax ,showResultMessage,isNotNull,openErrorNotification,getListDataMap} from '../../common';

import {UIDataListView} from '../../uicomponents/UIDataComponentView';

import UIFormView from '../../uicomponents/UIFormView';

import UIIcon from '../../uicomponents/UIIcon';

import UIActionBar from '../../uicomponents/UIActionBar';

import {List,Avatar} from 'antd';


/** 
 * 产品团队人员列表
*/
export default class ProductStafferListPanel extends React.Component {

	state = {
		modalTitle: "",
		disabledValues: {},
	}

    componentDidMount(){
		this.productDefineId = this.props.productDefineId;
		this.loadProductDefineData(this.productDefineId);
	}

	loadProductDefineData = (productDefineId) => {
		this.stafferList.loadData({productDefineId : productDefineId });
	}

	actionHandler = (actionEvent) => {
		const { productStafferEditForm, productStafferAddForm, stafferList, productDefineId } = this;
		return {
			refresh: () => {
				stafferList.loadData({ productDefineId: productDefineId });
			},
			doEdit: () => {
				this.setState({ modalTitle: "编辑产品设计成员[" + actionEvent.params.stafferName + "]" });
				setTimeout(() => {
					productStafferEditForm.openModal(function () {
						productStafferEditForm.setFormData(actionEvent.params);
					});
				});

			},
			addNew: () => {
				const productStaffers = stafferList.getListData();
				let stafferIds = productStaffers.map((productStaffer) => { return productStaffer.stafferId });
				this.setState({ modalTitle: "新增产品设计成员", disabledValues: { stafferId: stafferIds } });
				setTimeout(() => {
					productStafferAddForm.openModal();
				});
			},
			doSave: () => {
				let formRef = productStafferEditForm.isOpend() ? productStafferEditForm : productStafferAddForm;

				let url = productStafferEditForm.isOpend() ? 'PRODUCT/REQ/ProductDefine/saveProductTeam' : 'PRODUCT/REQ/ProductDefine/batchAddProductTeams';

				formRef.getFormData((formData) => {
					formData.productDefineId = productDefineId;
					ajax(url, formData).then(function (data) {
						formRef.closeModal();
						stafferList.loadData({ productDefineId: productDefineId });
					}).catch(function (message) {
						openErrorNotification(message);
					});
				});
			},
			doDelete: () => {
				ajax('PRODUCT/REQ/ProductDefine/deleteProductTeams', { ids: [actionEvent.params.id] }).then((data) => {
					stafferList.loadData({ productDefineId: productDefineId });
				}).catch(function (message) {
					openErrorNotification(message);
				});
			}
		};
	}

	render() {
		const stafferRoleMap = getListDataMap('ProductUI:product.teamStafferRoleList');

		return (
			<React.Fragment>
				<UIDataListView
					ref={(inst) => this.stafferList = inst}
					actionGroupId="ProductUI:productStafferListActionGroup"
					dataUrl="PRODUCT/REQ/ProductDefine/queryProductTeamList"
					bordered={false}
					actionHandler={this.actionHandler}
					renderListItem={(staffer) => {
						return (
							<List.Item actions={[
								<UIActionBar actionGroupId="ProductUI:productStafferActionGroup" data={staffer} actionHandler={this.actionHandler}>
								</UIActionBar>
							]}>
								<List.Item.Meta
									avatar={staffer.stafferPhoto != null ? <Avatar src={staffer.stafferPhoto}></Avatar> : <Avatar><UIIcon icon="user" /></Avatar>}
									title={staffer.stafferName}
									description={<span><UIIcon icon="user"/>{stafferRoleMap[staffer.stafferRole].text}</span>}
								>
								</List.Item.Meta>
								<div>{staffer.remarks}</div>
							</List.Item>
						);
					}}
				/>
				<UIFormView key="productStafferEditForm" title={this.state.modalTitle} ref={(inst) => { this.productStafferEditForm = inst }}
					actionHandler={this.actionHandler}
					formConfigId="ProductUI:productStafferEditForm"
					actionGroupId="saveActionGroup"
					inPop={true}
					destroyOnClose={true}
				/>
				<UIFormView key="productStafferAddForm" title={this.state.modalTitle} ref={(inst) => { this.productStafferAddForm = inst }}
					actionHandler={this.actionHandler}
					formConfigId="ProductUI:productStafferAddForm"
					actionGroupId="ProductUI:saveActionGroup"
					inPop={true}
					destroyOnClose={true}
					disabledValues={this.state.disabledValues}
				/>
			</React.Fragment>
		);
	}
}
